from flask import Blueprint,jsonify,request,abort
from flask_restful import Api,Resource
from app.models import *
# main = Blueprint("main", __name__)
work = Api()
class Work1(Resource):
    def get(self):
        work = Work.query.all()
        return jsonify({"work": list(map(convert_list3, work))})
    def post(self):
        work_add_one = Work(
            request.json.get('jineng'),
            request.json.get('xueli'),
            request.json.get('kaozheng')
        )
        db.session.add(work_add_one)
        db.session.commit()
        return jsonify({'work_add_one': convert_list3(work_add_one)})
work.add_resource(Work1,"/work1/")
class Work2(Resource):
    def get(self,uid):
        work_one = Work.query.filter_by(id=uid).first()
        if work_one is None:
            abort(404)
        return jsonify({'work_one': convert_list3(work_one)})
    def put(self,uid):
        work_update = Work.query.get(uid)
        work_update.jineng = request.json.get('jineng')
        work_update.xueli = request.json.get('xueli')
        work_update.kaozheng = request.json.get('kaozheng')

        db.session.add(work_update)
        db.session.commit()
        return jsonify({'work_update': convert_list3(work_update)})
work.add_resource(Work2, "/work2/<int:uid>/")